using System.Xml;
using System;
using Godot;

namespace Views.Edit
{
    public class EditCanvas : Control
    {
        #region Fields

        private bool _isCanvasMove;
        private Vector2 _lastCanvasPosition;
        private Vector2 _lastMousePosition;
        private EditCanvasInteractive _interactive;
        private EditViewport _viewport;

        #endregion

        #region Propertys

        public EditViewport EditViewport
        {
            get
            {
                if (_viewport == null)
                    _viewport = GetParent<EditViewport>();
                return _viewport;
            }
        }
        public float Scale => RectScale.x;
        public Vector2 Size => RectSize * Scale;
        public Vector2 Position => RectPosition;
        public Rect2 Rect => new Rect2(Position, Size);
        public EditCanvasInteractive Interactive
        {
            get
            {
                if (_interactive == null)
                    _interactive = GetNode<EditCanvasInteractive>("Interactive");
                return _interactive;
            }
        }

        #endregion

        #region Public Funtions

        public Rect2 GetClampRect()
        {
            var viewportSize = EditViewport.RectSize;
            return new Rect2(-viewportSize * 0.5f, Size);
        }
        public bool BeginCanvasMove(Vector2 mousePosition)
        {
            _lastCanvasPosition = Position;
            _lastMousePosition = mousePosition;
            _isCanvasMove = true;
            return _isCanvasMove;
        }
        public bool EndCanvasMove()
        {
            _isCanvasMove = false;
            return _isCanvasMove;
        }
        public void CanvasMoveing(Vector2 mousePosition)
        {
            SetPosition(_lastCanvasPosition + (mousePosition - _lastMousePosition));
        }
        public float CanvasZoomIn(bool isScreenChenter)
        {
            return SetScale(Scale * 1.414213562f, isScreenChenter);
        }
        public float CanvasZoomOut(bool isScreenChenter)
        {
            return SetScale(Scale / 1.414213562f, isScreenChenter);
        }
        public void ResetScale()
        {
            RectScale = Vector2.One;
            SetPosition((EditViewport.RectSize - RectSize) * 0.5f);
        }
        public float SetScale(float value, bool isScreenChenter)
        {
            if (value < 0.125f || value > 128) return Scale;
            Vector2 mousePosition;
            if (!isScreenChenter)
                mousePosition = GetLocalMousePosition();
            else
                mousePosition = ((EditViewport.RectSize * 0.5f) - Position) / Scale;
            var offset = mousePosition * (value - Scale);
            RectScale = new Vector2(value, value);
            SetPosition(Position - offset);
            this.EmitSignal("item_rect_changed");
            Update();
            return value;
        }

        internal void ApplySelect(Rect2 selectRect)
        {
            
        }

        public void SetPosition(Vector2 position)
        {
            var (clampXMin, clampYMin, clampXMax, clampYMax) = GetClampRect().ToLTRBTuple();

            if (position.x > -clampXMin)
                position.x = -clampXMin;
            else if (position.x < -clampXMax)
                position.x = -clampXMax;

            if (position.y > -clampYMin)
                position.y = -clampYMin;
            else if (position.y < -clampYMax)
                position.y = -clampYMax;

            //Debug.Log("SetCanvasPosition", position, clampXMin, clampYMin, clampXMax, clampYMax);
            RectPosition = position;

        }

        #endregion

        #region Godot Override API

        public override bool _ClipsInput()
        {
            return true;
        }

        #endregion
    }
}